using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dimok.Data.Database;

namespace Data.DataSources {
	public class StateFlow_ActionsDataSource :ObjectDataSourceBase {
		public override string SelectViewName { get { return "v_StateFlow_Actions"; } }
		public override string InsertViewName { get { return "t_StateFlow_Actions"; } }
		public StateFlow_ActionsDataSource() : base() {
			SelectMethod = "SelectSFA";
		}

		#region Overrides
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);
			SetStateIDParameter(StateID);
			SetTableNameParameter(TableName);
			OrderBy.Add("StateFromPos,StateToPos,Priority");
		}

		#endregion

		#region Parameters
		private const int DEFAULT_ID = -1;
		private const string STATE_ID = "StateID";
		private const string TABLE_NAME = "TableName";
		public int StateID = DEFAULT_ID;
		private string _tableName = "";
		private int _tableNameStateID = DEFAULT_ID;
		public string TableName {
			get {
				if(StateID == DEFAULT_ID) return "";
				if(_tableName == "" || _tableNameStateID != GetStateIDParameter()) {
					_tableName = SQL.ExecuteScalar("SELECT TableName FROM t_State WHERE ID = @StateID", StateID) + "";
					_tableNameStateID = GetStateIDParameter();
				}
				return _tableName;
			}
			set {
				SetTableNameParameter(_tableName = value);
			}
		}
		private void RemStateIDParameter(){ RemParameter(SelectParameters,STATE_ID);}

		private int GetStateIDParameter(){
			string param = GetParameter(SelectParameters,STATE_ID);
			return param == null ? DEFAULT_ID : int.Parse(param);
		}

		public void SetStateIDParameter(int stateID) {
			if(stateID == StateID) return;
			StateID = stateID;
			SetParameter(SelectParameters,TypeCode.Int32,STATE_ID,stateID);
			if(stateID != DEFAULT_ID) SetTableNameParameter("");
		}
		public void SetTableNameParameter(string tableName) {
			if(tableName == GetParameter(SelectParameters, TABLE_NAME)) return;
			if(tableName == "" || GetStateIDParameter() != DEFAULT_ID )
				RemParameter(SelectParameters, TABLE_NAME);
			else {
				SetParameter(SelectParameters, TypeCode.Object, TABLE_NAME, tableName);
				RemStateIDParameter();
			}
		}

		public override string Filter {
			get {
				List<string> filter = new List<string>();
				if(StateID != DEFAULT_ID) filter.Add(STATE_ID + "=" + StateID);
				if(TableName != "") filter.Add(TABLE_NAME + "=" + TableName);
				return string.Join(",",filter.ToArray());
			}
		}

		#endregion

		#region Data Access
		public DataView SelectSFA() { return SelectEx(); }
		public DataView SelectSFA(string TableName) { return SelectSFA(TableName, ""); }
		public DataView SelectSFA(string TableName, string Filter) {
			Where.Add(Filter);
			return Select("TableName = @TableName", TableName);
		}
		public DataView SelectSFA(int StateID) { return SelectSFA(StateID, ""); }
		public DataView SelectSFA(int StateID, string Filter) {
			if(StateID == DEFAULT_ID) return SelectEx();
			Where.Add(Filter);
			return Select("@StateID IN(StateFromID,StateToID)",StateID);
		}
		public void DeleteEx(int StateFromID,int StateToID,int StateFlowActionID) {
			SQL.ExecuteNonQuery("DELETE FROM t_StateFlow_Actions WHERE StateFromID=@StateFromID AND StateToID=@StateToID AND StateFlowActionID=@StateFlowActionID",
				StateFromID,StateToID,StateFlowActionID);
		}
		#endregion

	}
}